package cn.libinal.scloud.base.service.impl;

import cn.libinal.scloud.base.common.service.impl.AbstractJpaServiceImpl;
import cn.libinal.scloud.base.entity.Permission;
import cn.libinal.scloud.base.entity.Role;
import cn.libinal.scloud.base.repository.PermissionRepository;
import cn.libinal.scloud.base.repository.RoleRepository;
import cn.libinal.scloud.base.service.PermissionService;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.querydsl.core.types.Predicate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * @author libinal
 * created at 2018/11/6 20:55
 */
@Service
public class PermissionServiceImpl extends AbstractJpaServiceImpl<Permission, PermissionRepository> implements PermissionService {
    private final RoleRepository roleRepository;
    public PermissionServiceImpl(PermissionRepository repository, RoleRepository roleRepository) {
        super(repository);
        this.roleRepository = roleRepository;
    }

    @Override
    public Predicate buildPagingPredicates(Permission permission) {
        return null;
    }

    @Override
    public Set<Permission> getRolePermission(String roleId) {
        Optional<Role> optional = roleRepository.findById(roleId);
        if (optional.isPresent()) {
            return optional.get().getPermissions();
        }
        return Sets.newLinkedHashSet();
    }
}
